﻿using System.Windows;
using System.Windows.Controls;
using mal.WP7.FreeRemote.ViewModels;
using Microsoft.Phone.Controls;
using System.Windows.Media;

namespace mal.WP7.FreeRemote
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region Fields
        private bool remotePanelVisible = true;
        private bool storyboardRunning = false;
        #endregion

        #region Construction / Destruction
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.DrawButtons();

            SolidColorBrush backgr = (SolidColorBrush)Application.Current.Resources["PhoneBackgroundBrush"];
            if (backgr.Color != Colors.Black)
            {
                this.navigationName.Visibility = System.Windows.Visibility.Collapsed;
                this.navigationNameWhite.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.navigationName.Visibility = System.Windows.Visibility.Visible;
                this.navigationNameWhite.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties

        #endregion

        #region Methods
        private void RunSwipe()
        {
            if (!this.storyboardRunning)
            {
                this.storyboardRunning = true;
                if (this.remotePanelVisible)
                {
                    this.configPanel.Begin();
                }
                else
                {
                    this.remotePanel.Begin();
                }
            }
        }

        private void DrawButtons()
        {
            this.DrawButton(0, 0, "av");
            this.DrawButton(2, 0, "power");

            int value = 1;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    this.DrawButton(j, 1 + i, value.ToString());
                    value++;
                }
            }

            this.DrawButton(1, 4, "0");

            this.DrawButton(0, 5, "red");
            this.DrawButton(1, 5, "up");
            this.DrawButton(2, 5, "blue");

            this.DrawButton(0, 6, "left");
            this.DrawButton(1, 6, "ok");
            this.DrawButton(2, 6, "right");

            this.DrawButton(0, 7, "green");
            this.DrawButton(1, 7, "down");
            this.DrawButton(2, 7, "yellow");

            this.DrawButton(0, 8, "home", 3);

            this.DrawButton(0, 9, "vol_inc");
            this.DrawButton(1, 9, "mute");
            this.DrawButton(2, 9, "prgm_inc");

            this.DrawButton(0, 10, "vol_dec");
            this.DrawButton(1, 10, "rec");
            this.DrawButton(2, 10, "prgm_dec");

            this.DrawButton(0, 11, "prev");
            this.DrawButton(1, 11, "play");
            this.DrawButton(2, 11, "fwd");
        }

        private void DrawButton(int column, int line, string caption)
        {
            this.DrawButton(column, line, caption, 1);
        }

        private void DrawButton(int column, int line, string caption, int size)
        {
            int buttonSize = 133;

            Style style = (Style)this.Resources["buttonStyle"];

            Button button = new Button();
            button.Style = style;
            button.Margin = new Thickness(column * buttonSize, line * buttonSize, 0, 0);
            button.Width = size * buttonSize;
            button.Height = buttonSize;
            button.Tag = caption;

            button.Click += new RoutedEventHandler(this.OnButtonClick);
#if DEBUG
            button.Background = new SolidColorBrush(Colors.Red);
            button.Content = caption;
#endif
            contentButton.Children.Add(button);
        }

        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            ((MainViewModel)this.DataContext).SendCommand((string)((Button)sender).Tag);
        }

        #endregion

        #region Events
        private void OnNavigationNameClick(object sender, System.Windows.RoutedEventArgs e)
        {
            this.RunSwipe();
        }

        private void OnConfigPanelCompleted(object sender, System.EventArgs e)
        {
            this.storyboardRunning = false;
            this.remotePanelVisible = false;
        }

        private void OnRemotePanelCompleted(object sender, System.EventArgs e)
        {
            this.storyboardRunning = false;
            this.remotePanelVisible = true;
        }

        private void OnGestureListenerFlick(object sender, FlickGestureEventArgs e)
        {
            if (e.Direction == System.Windows.Controls.Orientation.Horizontal)
            {
                if (this.remotePanelVisible)
                {
                    if (e.HorizontalVelocity < 0)
                    {
                        this.RunSwipe();
                    }
                }
                else
                {
                    if (e.HorizontalVelocity > 0)
                    {
                        this.RunSwipe();
                    }
                }
            }
        }

        private void OnGestureListenerDragStarted(object sender, DragStartedGestureEventArgs e)
        {
            //// this.isDraged = true;
        }

        private void OnGestureListenerDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            //// if (this.isDraged)
            ////{
            ////    panelTranslate.X += e.HorizontalChange;
            ////}
        }

        private void OnGestureListenerDragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
            ////this.isDraged = false;
            ////this.RunSwipe();
        }
        #endregion
    }
}